from enum import Enum
from typing import List, Optional

from bct.tools.common.bct4.base_dto import JsonDTO


class LcmEventType(Enum):
    OPEN = "OPEN"
    SETTLE = "SETTLE"
    UNWIND = "UNWIND"
    KNOCK_IN = "KNOCK_IN"
    COUPON = "COUPON"
    DIVIDEND = "DIVIDEND"
    UNKNOWN = "UNKNOWN"


class CashFlowDTO(JsonDTO):
    payment_date: str
    cash_flow_type: str
    amount: str
    business_type: str
    payment_method: str
    cash_flow_category: str

    def __init__(self, param: dict = None):
        if param:
            self.payment_date = param.get("param", "")
            self.cash_flow_type = param.get("cashFlowType", "")
            self.amount = param.get("amount", "0")
            self.business_type = param.get("businessType", "")
            self.payment_method = param.get("paymentMethod", "")
            self.cash_flow_category = param.get("cashFlowCategory", "")

    def to_dict(self) -> dict:
        return {
            'payment_date': self.payment_date,
            'cash_flow_type': self.cash_flow_type,
            'amount': self.amount,
            'business_type': self.business_type,
            'payment_method': self.payment_method,
            'cash_flow_category': self.cash_flow_category
        }


class LcmDTO(JsonDTO):
    cash_flows: List[CashFlowDTO]
    event_id: str
    event_type: str
    trade_id: str
    created_at: str
    update_at: str
    created_by: str
    approval_status: str
    trade_record_id: str
    unwind_confirmation_id: Optional[str]

    def get_event_type(self) -> LcmEventType:
        for i in LcmEventType:
            if i.name == self.event_type:
                return i
        print("unknown type %s" % self.event_type)
        return LcmEventType.UNKNOWN

    def __init__(self, param: dict = None):
        if param:
            self.event_id = param.get("eventId", "")
            self.event_type = param.get("eventType", "")
            self.trade_id = param.get("tradeId", "")
            self.created_at = param.get("createdAt", "")
            self.update_at = param.get("updateAt", "")
            self.trade_record_id = param.get("tradeRecordId", "")
            self.created_by = param.get("createdBy", "")
            self.approval_status = param.get("approvalStatus", "")
            self.unwind_confirmation_id = param.get("unwindConfirmationId", None)
            self.cash_flows = []
            if "cashFlows" in param and isinstance(param['cashFlows'], List):
                for f in param['cashFlows']:
                    self.cash_flows.append(CashFlowDTO(f))

    def to_dict(self) -> dict:
        ret = {
            "cashFlows": [],
            "eventId": self.event_id,
            "eventType": self.event_type,
            "tradeId": self.trade_id,
            "createdAt": self.created_at,
            "updateAt": self.update_at,
            "createdBy": self.created_by,
            "approvalStatus": self.approval_status,
            "unwindConfirmationId": self.unwind_confirmation_id
        }
        if self.cash_flows and len(self.cash_flows) > 0:
            for i in self.cash_flows:
                ret['cashFlows'].append(i.to_dict())
        return ret
